package com.rainwood.material.uitls;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.DisplayMetrics;
import android.view.Surface;


/**
 * @author :
 * desc   :
 * version: 1.0
 */
public class ScreenAdapter {

    private static float sNonCompatDensity;
    private static float sNonCompatScaleDensity;
    private static float density;

    public static void setDensity(Activity activity, final Application application) {

        DisplayMetrics appDisplayMetrics = application.getResources().getDisplayMetrics();
        if (sNonCompatDensity == 0) {
            density = sNonCompatDensity = appDisplayMetrics.density;
            sNonCompatScaleDensity = appDisplayMetrics.scaledDensity;
            application.registerComponentCallbacks(new ComponentCallbacks() {
                @Override
                public void onConfigurationChanged(Configuration newConfig) {
                    //字体改变后,将appScaledDensity重新赋值
                    if (newConfig != null && newConfig.fontScale > 0) {
                        sNonCompatScaleDensity = application.getResources().getDisplayMetrics().scaledDensity;
                    }
                }

                @Override
                public void onLowMemory() {

                }
            });
        }
        final float targetDensity;
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
        //if(getScreenOriatation(activity)==Configuration.ORIENTATION_LANDSCAPE){
            final DisplayMetrics activityDisplayMetrics = activity.getResources().getDisplayMetrics();
            density = activityDisplayMetrics.density;
        }else{
            targetDensity = appDisplayMetrics.widthPixels / 375f;
            final float targetScaleDensity = targetDensity * (sNonCompatScaleDensity / sNonCompatDensity);
            final int targetDensityDpi = (int) (160 * targetDensity);

            final DisplayMetrics activityDisplayMetrics = activity.getResources().getDisplayMetrics();
            density = activityDisplayMetrics.density = targetDensity;
            activityDisplayMetrics.scaledDensity = targetScaleDensity;
            activityDisplayMetrics.densityDpi = targetDensityDpi;
        }
    }

    /**
     * 获取状态栏高度
     *
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    /**
     * px 转 dp
     *
     * @param px
     * @return
     */
    public static float px2dp(int px) {
        return Math.round(px / density);
    }

    /**
     * dp 转 px
     *
     * @param dp
     * @return
     */
    public static float dp2px(int dp) {
        return Math.round(dp * density);
    }
    public static int dpToPx(int dp) {
        return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
    }
    public static Bitmap getRotateBitmap(Bitmap b, float rotateDegree) {
        if (b == null) {
            return null;
        }
        Matrix matrix = new Matrix();
        matrix.postRotate(rotateDegree);
        return Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, false);
    }
}
